package com.hz.module.system.controller.admin.check;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.hz.framework.common.pojo.PageParam;
import com.hz.framework.common.pojo.PageResult;
import com.hz.framework.common.pojo.CommonResult;
import com.hz.framework.common.util.object.BeanUtils;
import static com.hz.framework.common.pojo.CommonResult.success;

import com.hz.framework.excel.core.util.ExcelUtils;

import com.hz.framework.apilog.core.annotation.ApiAccessLog;
import static com.hz.framework.apilog.core.enums.OperateTypeEnum.*;

import com.hz.module.system.controller.admin.check.content.vo.*;
import com.hz.module.system.dal.dataobject.check.content.CheckContentDO;
import com.hz.module.system.service.check.content.CheckContentService;

@Tag(name = "管理后台 - 检查内容清单")
@RestController
@RequestMapping("/system/check-content")
@Validated
public class CheckContentController {

    @Resource
    private CheckContentService checkContentService;

    @PostMapping("/create")
    @Operation(summary = "创建检查内容清单")
    @PreAuthorize("@ss.hasPermission('system:check-content:create')")
    public CommonResult<String> createCheckContent(@Valid @RequestBody CheckContentSaveReqVO createReqVO) {
        return success(checkContentService.createCheckContent(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新检查内容清单")
    @PreAuthorize("@ss.hasPermission('system:check-content:update')")
    public CommonResult<Boolean> updateCheckContent(@Valid @RequestBody CheckContentSaveReqVO updateReqVO) {
        checkContentService.updateCheckContent(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除检查内容清单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:check-content:delete')")
    public CommonResult<Boolean> deleteCheckContent(@RequestParam("id") String id) {
        checkContentService.deleteCheckContent(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得检查内容清单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:check-content:query')")
    public CommonResult<CheckContentRespVO> getCheckContent(@RequestParam("id") String id) {
        CheckContentDO checkContent = checkContentService.getCheckContent(id);
        return success(BeanUtils.toBean(checkContent, CheckContentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得检查内容清单分页")
    @PreAuthorize("@ss.hasPermission('system:check-content:query')")
    public CommonResult<PageResult<CheckContentRespVO>> getCheckContentPage(@Valid CheckContentPageReqVO pageReqVO) {
        PageResult<CheckContentDO> pageResult = checkContentService.getCheckContentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CheckContentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出检查内容清单 Excel")
    @PreAuthorize("@ss.hasPermission('system:check-content:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCheckContentExcel(@Valid CheckContentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CheckContentDO> list = checkContentService.getCheckContentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "检查内容清单.xls", "数据", CheckContentRespVO.class,
                        BeanUtils.toBean(list, CheckContentRespVO.class));
    }

}